﻿using System;
using System.Collections.Generic;
using tour.szhome.common;
using tour.szhome.map.DTO;
using TOUR.SZHOME.DAL;
using TOUR.SZHOME.Entity;
namespace TOUR.SZHOME.BLL
{
    /// <summary>
    /// 数据表Projects的业务逻辑类
    /// </summary>
    public class ProjectsBLL
    {
        public List<ProjectsEntity> GetProjectList(string name, PageSet pageSet) 
        {
            string where = string.Empty;
            if (!string.IsNullOrEmpty(name))
            {
                where += " XMMC like '%" + name + "%'";
            }
            // 分页助手处理
            pageSet.RecordCount = ProjectsDAL.SearchCount(where);
            pageSet.PageCount = (int)Math.Ceiling(pageSet.RecordCount / (pageSet.PageSize * 1.0));
            return ProjectsDAL.Search(where, "XMID", (pageSet.PageIndex - 1) * pageSet.PageSize, pageSet.PageSize);
        }

        /// <summary>
        /// 获取所有项目列表，前台用
        /// </summary>
        /// <returns></returns>
        public List<ProjectsEntity> GetAllProject() 
        {
            return ProjectsDAL.Search("1=1", "XMID",20);
        }

        /// <summary>
        /// 根据ID获取对象
        /// </summary>
        /// <param name="projectId"></param>
        /// <returns></returns>
        public ProjectsEntity GetProjectByID(int projectId) 
        {
            return ProjectsDAL.GetByXMID(projectId);
        }

        /// <summary>
        /// 根据ID删除对象
        /// </summary>
        /// <param name="nid"></param>
        /// <returns></returns>
        public bool DelByID(int nid)
        {
            return ProjectsDAL.Delete(nid);
        }

        /// <summary>
        /// 编辑动态的逻辑方法
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public bool EditProject(ProjectsEntity model)
        {
            if (model.XMID > 0)
            {
                return ProjectsDAL.Update(model);
            }
            else
            {
                return ProjectsDAL.Insert(model);
            }
        }

        /// <summary>
        /// 获取项目列表 --  前台
        /// </summary>
        /// <param name="XMLB"></param>
        /// <param name="WYLX"></param>
        /// <returns></returns>
        public List<ProjectsEntity> GetToWeb(string XMLB, string WYLX)
        {
            return ProjectsDAL.GetToWeb(XMLB, WYLX);
        }

        /// <summary>
        /// 为首页下部分获取拼接数据
        /// </summary>
        /// <param name="isTJ">是否推荐</param>
        /// <returns></returns>
        public List<ProjectDTO> GetToWebIndex(int isTJ, string XMLB)
        {
            return ProjectsDAL.GetToWebIndex(isTJ, XMLB);
        }

        /// <summary>
        /// 取得相应条件的项目总数
        /// </summary>
        /// <param name="XMLB"></param>
        /// <param name="WYLX"></param>
        /// <returns></returns>
        public int GetCount(string XMLB, string WYLX)
        {
            string where = " 1 = 1 ";
            if (!string.IsNullOrEmpty(XMLB))
            {
                where += " and XMLB = '" + XMLB + "'";
            }
            if (!string.IsNullOrEmpty(WYLX))
            {
                where += " and WYLX like '%" + WYLX + "%' ";
            }
            return ProjectsDAL.SearchCount(where);
        }

        /// <summary>
        /// 根据ID获取项目名称
        /// </summary>
        /// <param name="pid"></param>
        /// <returns></returns>
        public string GetProjectNameByID(int pid)
        {
            return ProjectsDAL.GetNameByID(pid);
        }
    }
}
